/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.uac.dao.IEntityTypeManageDao;
import com.eelpo.cmmp.uac.dao.entity.TEntityType;
import com.eelpo.cmmp.uac.dao.vo.EntityType;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IEntityTypeManageService;
import com.eelpo.common.util.BlankUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * 
 * 实体类型管理服务具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Service
public class EntityTypeManageService implements IEntityTypeManageService {

	@Resource
	protected IEntityTypeManageDao entityTypeManageDao;

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#getEntityTypeById
	 *      (java.lang.String)
	 */
	@Override
	public EntityType getEntityTypeById(String id) throws ServiceException {

		TEntityType tentityType = entityTypeManageDao.getEntityTypeById(id);

		if (BlankUtils.isBlank(tentityType)) {
			throw new ServiceException("查询的实体类型不存在！");
		}

		return po2vo(tentityType);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#getEntityTypeByCode
	 *      (java.lang.String)
	 */
	@Override
	public EntityType getEntityTypeByCode(String code) throws ServiceException {

		TEntityType tentityType = entityTypeManageDao.getEntityTypeByCode(code);

		if (BlankUtils.isBlank(tentityType)) {
			throw new ServiceException("查询的实体类型[" + code + "]不存在！");
		}

		return po2vo(tentityType);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#checkExist(java
	 *      .lang.String)
	 */
	@Override
	public boolean checkExist(String code) throws ServiceException {
		return entityTypeManageDao.checkExist(code);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#addEntityType(com
	 *      .eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public String addEntityType(EntityType entityType) throws ServiceException {

		// 检查实体类型是否已经存在
		if (entityTypeManageDao.checkExist(entityType.getCode())) {
			throw new ServiceException("实体类型代码[" + entityType.getCode() + "]已经存在！");
		}

		return entityTypeManageDao.addEntityType(vo2po(entityType));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#updateEntityType
	 *      (com.eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public void updateEntityType(EntityType entityType) throws ServiceException {

		// 检查实体类型是否已经存在
		if (entityTypeManageDao.checkExistByUpdate(entityType.getId(), entityType.getCode())) {
			throw new ServiceException("实体类型代码[" + entityType.getCode() + "]已经存在！");
		}

		entityTypeManageDao.updateEntityType(vo2po(entityType));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#disabledEntityType(com.eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public void disabledEntityType(EntityType entityType) throws ServiceException {

		TEntityType tentityType = entityTypeManageDao.getEntityTypeById(entityType.getId());

		tentityType.setStatus(Status.DISABLED.getValue());

		entityTypeManageDao.updateEntityType(tentityType);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#enabledEntityType(com.eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public void enabledEntityType(EntityType entityType) throws ServiceException {

		TEntityType tentityType = entityTypeManageDao.getEntityTypeById(entityType.getId());

		tentityType.setStatus(Status.ENABLED.getValue());

		entityTypeManageDao.updateEntityType(tentityType);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#deleteEntityType
	 *      (com.eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public void deleteEntityType(EntityType entityType) throws ServiceException {

		if (entityTypeManageDao.checkEntityTypeUsed(entityType.getId())) {
			throw new ServiceException("无法删除，该实体类型已经被使用！");
		}

		entityTypeManageDao.deleteEntityType(entityType.getId());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#getEntityTypesPaing
	 *      (com.eelpo.cmmp.uac.dao.vo.EntityType, int, int)
	 */
	@Override
	public List<EntityType> getEntityTypesPaing(EntityType entityType, int first, int max) throws ServiceException {
		return po2vo(entityTypeManageDao.getEntityTypesPaing(entityType, first, max));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IEntityTypeManageService#getEntityTypesCount
	 *      (com.eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public int getEntityTypesCount(EntityType entityType) throws ServiceException {
		return entityTypeManageDao.getEntityTypesCount(entityType);
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param entityType
	 *            实体类型值对象
	 * @return 返回实体类型实体对象
	 */
	public static TEntityType vo2po(EntityType entityType) {

		TEntityType tentityType = new TEntityType();
		BeanUtils.copyProperties(entityType, tentityType, new String[] { "status" });

		tentityType.setStatus(Status.valueOf(entityType.getStatus()));

		return tentityType;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tentityType
	 *            实体类型实体对象
	 * @return 返回实体类型值对象
	 */
	public static EntityType po2vo(TEntityType tentityType) {
		EntityType entityType = new EntityType();
		BeanUtils.copyProperties(tentityType, entityType, new String[] { "status" });
		entityType.setStatus(Status.valueOf(tentityType.getStatus()));
		return entityType;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tentityTypes
	 *            实体类型实体对象列表
	 * @return 返回实体类型值对象列表
	 */
	public static List<EntityType> po2vo(List<TEntityType> tentityTypes) {
		List<EntityType> entityTypes = new ArrayList<EntityType>();
		for (TEntityType tentityType : tentityTypes) {
			entityTypes.add(po2vo(tentityType));
		}
		return entityTypes;
	}

}
